﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using Converter;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.Resources;
using System.Reflection;
using Accounting.Library;
using csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_KyGiaThanh;

namespace csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.GT_DoDangCK
{
    public partial class FormThongTin : ChildWindow
    {
        GT_DoDangCK_ListViewModel ListViewModel;
        GT_DoDangCK_ChildWindow gt_DoDangCK_ChildWindow;
        public event EventHandler Saved;
        public FormThongTin(GT_DoDangCK_ListViewModel listViewModel)
        {        
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("ccsla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.GT_DoDangCK.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            this.ListViewModel = listViewModel;

            System.Windows.Data.CollectionViewSource gt_KyGiaThanh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["GT_KyGiaThanh_ListViewModelSource"];
            GT_KyGiaThanh_ListViewModel gt_KyGiaThanh_ListViewModel = new GT_KyGiaThanh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            gt_KyGiaThanh_CollectionViewSource.Source = new List<object> { gt_KyGiaThanh_ListViewModel };

           
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void TinhDoDangCKButton_Click(object sender, RoutedEventArgs e)
        {

            if (KyGiaThanh_ComboBox.SelectedItem == null)
            {
                MessageBoxResult notice = MessageBox.Show("Bạn chưa nhập liệu đầy đủ", "Chưa nhập liệu", MessageBoxButton.OKCancel);
            }
            else
            {

                IList<GT_DoDangCKInfo> list = ListViewModel.Model.getListOfInfo();
                GT_DoDangCKInfo DoDangCK = null;
                foreach (GT_DoDangCKInfo item in list)
                {
                    if (item.KyGiaThanhInfo == ((GT_KyGiaThanhInfo)this.KyGiaThanh_ComboBox.SelectedItem).Ma)
                    {
                        DoDangCK = item;
                    }
                }
                if (DoDangCK != null)
                {

                    MessageBoxResult result = MessageBox.Show("Chứng từ đánh giá dở dang cuối kỳ này đã tồn tại, bạn có muốn xem hay không?", "Xem chứng từ đánh giá dở dang cuối kỳ", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.OK)
                    {

                        gt_DoDangCK_ChildWindow = new GT_DoDangCK_ChildWindow(DoDangCK.Khoa);
                        gt_DoDangCK_ChildWindow.Show();
                        gt_DoDangCK_ChildWindow.TinhDoDangCKCompleteted += new EventHandler(OnTinhDoDangCKCompleteted);
                        gt_DoDangCK_ChildWindow.OnClosedChildWindow += new EventHandler(OnClosedWindow);
                    }
                }
                else
                {

                    gt_DoDangCK_ChildWindow = new GT_DoDangCK_ChildWindow((GT_KyGiaThanhInfo)this.KyGiaThanh_ComboBox.SelectedItem, this.DienGiaiTextBox.Text);
                    gt_DoDangCK_ChildWindow.Show();
                    gt_DoDangCK_ChildWindow.Visibility = Visibility.Collapsed;

                    this.TinhDoDangCKItemButton.IsEnabled = false;
                    this.BoQuaItemButton.IsEnabled = false;
                    gt_DoDangCK_ChildWindow.TinhDoDangCKCompleteted += new EventHandler(OnTinhDoDangCKCompleteted);
                    gt_DoDangCK_ChildWindow.OnClosedChildWindow += new EventHandler(OnClosedWindow);
                }
            }
            
        }
        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            
            ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, null);
            
        }
        private void OnClosedWindow(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OnTinhDoDangCKCompleteted(object sender, EventArgs e)
        {
            if (gt_DoDangCK_ChildWindow.CountSave == 0)
            {

                MessageBoxResult result = MessageBox.Show("Chứng từ đánh giá dở dang cuối kỳ đã xong, bạn có muốn xem chứng từ đánh giá dở dang cuối kỳ không ?", "Xem chứng từ đánh giá dở dang cuối kỳ", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                {
                    gt_DoDangCK_ChildWindow.Visibility = Visibility.Visible;
                    gt_DoDangCK_ChildWindow.CountSave++;
                    if (Saved != null)
                    {
                        Saved(sender, e);
                    }

                }
                else
                {
                    gt_DoDangCK_ChildWindow.CountSave++;
                    gt_DoDangCK_ChildWindow.Close();
                    if (Saved != null)
                    {
                        Saved(sender, e);
                    }

                }
            }
            else
            {
                if (Saved != null)
                {
                    Saved(sender, e);
                }
            }
           
        }
        
    }
}

